package com.esp.app.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.RemoteViews;

import com.esp.app.air.AirplanModel;
import com.esp.app.air.ViewAirPlaneHandler;
import com.esp.app.bluetooth.BluetoothModel;
import com.esp.app.bluetooth.ViewBlueHandler;
import com.esp.app.brightness.BrightnessModel;
import com.esp.app.brightness.ViewBrightHandler;
import com.esp.app.gps.GpsModel;
import com.esp.app.gps.ViewGpsHandler;
import com.esp.app.wifi.ViewHandler;
import com.esp.app.wifi.WifiModel;

public class MyWidgetProvider extends AppWidgetProvider
{
	public static final String TOGGLE_WIFI_STATE = "com.esp.app.widget.wifi";
	public static final String TOGGLE_BLUETOOTH_STATE = "com.esp.app.widget.bluetooth";
	public static final String TOGGLE_GPS_STATE = "com.esp.app.widget.gps";
	public static final String TOGGLE_AIRPLANE_STATE = "com.esp.app.widget.airplane";
	
	private static final String LOG_TAG  = MyWidgetProvider.class.getName();
	
	@Override
	public void onEnabled(Context context) 
	{
		Log.i(LOG_TAG, "onEnabled()");
        super.onEnabled(context);
  
	}
	
	
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) 
	{
        Log.d(LOG_TAG, "onUpdate called with " + appWidgetIds.length + " widget ids");
		
		final int N = appWidgetIds.length;
		
		for (int i = 0; i < N; i++) 
		{
			int appWidgetId = appWidgetIds[i];

			 
			RemoteViews views = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);
			
			//start image capture
			Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			PendingIntent pendingPicIntent = PendingIntent.getActivity(context, 0,picIntent, 0);
			views.setImageViewResource(R.id.btn_picture,R.drawable.camera_picture);
			views.setOnClickPendingIntent(R.id.btn_picture, pendingPicIntent);
			
			// start video recorder 
			Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
			PendingIntent pendingVideoIntent = PendingIntent.getActivity(context, 0,videoIntent, 0);
			views.setImageViewResource(R.id.btn_video,R.drawable.camera_video);
			views.setOnClickPendingIntent(R.id.btn_video, pendingVideoIntent);

			appWidgetManager.updateAppWidget(appWidgetId, views);

		}
	
        
        
		//TODO : UPDATE FOR WIFI
        final Boolean state = WifiModel.getWifiState(context);
        final int wifiResource = deriveWifiResource(state);
        updateWifiView(context, wifiResource);
	
      //TODO : UPDATE FOR BlueTooth
        final Boolean blueState = BluetoothModel.getBluetoothState(context);
        final int blueResouce = deriveBluetoothResource(blueState);
        //ViewBlueHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds, blueResouce);
        updateBlutoothView(context,blueResouce);
        
       //TODO UPDATE FOR AirPlane 
        final Boolean airplaneState = AirplanModel.getAirModeState(context); 
        final int airResouce = deriveAirplaneResource(airplaneState);
        //ViewAirPlaneHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds, airResouce);
        updateAirPlaneView(context,airResouce);
        
        //TODO UPDATE FOR Brightness
        final int bightness = BrightnessModel.getBrightness(context);
        ViewBrightHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds, bightness);
        
        //TODO UPDATE FOR GPS
        final int gpsResource = GpsModel.getGPSStatus(context);
        ViewGpsHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds, gpsResource);
        
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		
		final String action = intent.getAction();
		Log.d(LOG_TAG, "onReceive |=>  " + action);
		boolean handled = false;
	
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			handled = true;
			handleAppWidgetDeleted(context, intent);
		}
		
		//TODO It's called when you change wifi from setting 
		if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
			handled = true;
			final Boolean state = WifiModel.getWifiState(context);
			final int resource = deriveWifiResource(state);
			updateWifiView(context, resource);
		}

		//TODO It's called when you change wifi from our widget 
		if (TOGGLE_WIFI_STATE.equals(action)) {
			handled = true;
			final Boolean wifiStatus = WifiModel.getWifiState(context);
			// ignore toggle requests if the wifi is currently changing state
			if (wifiStatus != null) {
				WifiModel.setWifiState(context, !wifiStatus);
				updateWifiView(context, R.drawable.wifi_enabled);
			}
		}
		
		
		//TODO It's called when you change bluetooth from setting
		
		if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
			handled = true;
			final Boolean state = BluetoothModel.getBluetoothState(context);
			final int resource = deriveBluetoothResource(state);
			updateBlutoothView(context, resource);
		}
		//TODO It's called when you change Bluetooth from our widget 
		
		if (TOGGLE_BLUETOOTH_STATE.equals(action)) {
			handled = true;
			final Boolean BlutoothState  = BluetoothModel.getBluetoothState(context);
			if (BlutoothState != null) {
				BluetoothModel.setBluetoothState(context);
				updateBlutoothView(context, R.drawable.bluetooth_enabled);
			}
		}
		
		//TODO It's called when you change airplane mode from setting
		if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
			handled = true;
			final Boolean airState = AirplanModel.getAirModeState(context);
			final int resource = deriveAirplaneResource(airState);
			updateAirPlaneView(context, resource);
		}
		
		//TODO It's called when you change AirMode from our widget
		if (TOGGLE_AIRPLANE_STATE.equals(action)) {
			handled = true;
			final Boolean airState = AirplanModel.getAirModeState(context);
			if (airState != null) {
				AirplanModel.setAirModeState(context);
				updateAirPlaneView(context, R.drawable.wifichanging);
			}
		}
		
		
		//TODO It's called when you change brightness from our widget
		if(BrightnessModel.BRIGHTNESS_CHANGED_ACTION.equals(action))
		{
			handled = true;
			 final int bightness = BrightnessModel.getBrightness(context);
			 updateBrightnessView(context, bightness);
			
		}
		
		//TODO It's called when you change GPS  from our widget
		if(GpsModel.GPS_CHANGED_ACTION.equals(action)){
			handled = true;
			final int gpsResource = GpsModel.getGPSStatus(context);
			updateGPSView(context, gpsResource);
		}
		
		if (!handled) {
			super.onReceive(context, intent);
		}

	}
	
	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		Log.d(LOG_TAG, "onDisabled");
	}
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Log.d(LOG_TAG, "onDeleted");
		
	}
	
	private void handleAppWidgetDeleted(final Context context,
			final Intent intent) {

		final Bundle extras = intent.getExtras();
		final int appWidgetId = extras.getInt(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
			this.onDeleted(context, new int[] { appWidgetId });
		}

	}

	private void updateWifiView(final Context context, final int resource) {

		final AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		final ComponentName thisWidget = new ComponentName(context,
				this.getClass());
		final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		ViewHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds,
				resource);

	}

	private int deriveWifiResource(final Boolean state) {
		return state == null ? R.drawable.wifi_disabled
				: state ? R.drawable.wifi_enabled : R.drawable.wifi_disabled;
	}
	
	private int deriveAirplaneResource(final Boolean state) {
		return state == null ? R.drawable.wifichanging
				: state ? R.drawable.wifion : R.drawable.wifioff;
	}
	
	private int deriveBluetoothResource(final Boolean state) {
		return state == null ? R.drawable.bluetooth_enabled
				: state ? R.drawable.bluetooth_enabled : R.drawable.bluetooth_disabled;
	}
	
	
	
	
	private void updateBlutoothView(final Context context, final int resource){
		
		final AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		final ComponentName thisWidget = new ComponentName(context,
				this.getClass());
		final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		ViewBlueHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds,
				resource);
	}
	
	private void updateAirPlaneView(final Context context, final int resource){
		
		final AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		final ComponentName thisWidget = new ComponentName(context,
				this.getClass());
		final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		ViewAirPlaneHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds,
				resource);
	}
	    
	private void updateBrightnessView(final Context context, final int resource){
		
		final AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		final ComponentName thisWidget = new ComponentName(context,
				this.getClass());
		final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		ViewBrightHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds,
				resource);
	}
	
	private void updateGPSView(final Context context, final int resource){
		
		final AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		final ComponentName thisWidget = new ComponentName(context,
				this.getClass());
		final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		ViewGpsHandler.updateAppWidgets(context, appWidgetManager, appWidgetIds,
				resource);
	}
	
}
